package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.pojo.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Api(value = "套餐管理")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    @PostMapping
    @ApiOperation(value = "套餐添加")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
    public Result<String> add(@RequestBody SetmealDTO setmealDTO){
        setMealService.add(setmealDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<PageResult> findByPage(SetmealPageQueryDTO queryDTO){
        PageResult result = setMealService.findByPage(queryDTO);
        return Result.success(result);
    }

    @DeleteMapping
    @ApiOperation(value = "套餐删除")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> deleteByIds(@RequestParam List<Long> ids){
        setMealService.deleteByIds(ids);
        return Result.success();
    }

    @PutMapping
    @ApiOperation(value = "修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> update(@RequestBody SetmealDTO setmealDTO){
        setMealService.update(setmealDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询套餐")
    public Result<SetmealVO> findById(@PathVariable Long id){
        SetmealVO setmealVO = setMealService.findById(id);
        return Result.success(setmealVO);
    }

    @PostMapping("status/{status}")
    @ApiOperation(value = "套餐起售，停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> openAndClose(@PathVariable Integer status,Long id){
        setMealService.openAndClose(status,id);
        return Result.success();
    }
}
